<?php

namespace app\service;

class TaskRepairService
{

    public function __construct()
    {
    }

    /**
     * 返修情况
     * @param $projectId
     * @param $stepCategoryId
     * @param $startTime
     * @param $endTime
     * @param string $dataType
     * @return array[]|int[]
     */
    public function getEffectedTaskInfo($projectId, $stepCategoryId, $startTime, $endTime, string $dataType = 'count'): array
    {
        $field = [
            'roat.task_id',
            'tro.repair_step_category_id as tro_repair_step_category_id',
            'roat.step_category_id as roat_step_category_id',
        ];

        $taskRepairOrderModel = (new \app\model\TaskRepairOrderModel)->alias('tro')->field($field);
        $taskRepairOrderModel->join('repair_order_affected_task roat', 'tro.id = roat.task_repair_order_id');
        $taskRepairOrderModel->join('task', 'task.id = roat.task_id');
        if (!empty($stepCategoryId)) {
            $taskRepairOrderModel->whereIn('tro.repair_step_category_id', $stepCategoryId);
        }
        if (!empty($projectId)) {
            $taskRepairOrderModel->where('tro.project_id', $projectId);
        }
        $closedId = StatusService::getInstance()->byCode('closed')['id'];
        $list = $taskRepairOrderModel
            ->whereBetween('tro.created', [$startTime, $endTime])
            ->whereFieldRaw("get_json_string(task.`json`,'task_status')", '<>', $closedId)
            ->group('roat.task_id,tro.repair_step_category_id,roat.step_category_id')
            ->select()
            ->toArray();
        if (empty($list)) {
            return ['upstream_step_category_task_count' => 0, 'step_category_task_count' => 0, 'upstream_step_category_task_list' => [], 'step_category_task_list' => []];
        }
        $stepCategoryTaskCount = 0;
        $upstreamStepCategoryTaskCount = 0;
        if ($dataType == 'count') {
            foreach ($list as $item) {
                if ($item['tro_repair_step_category_id'] == $item['roat_step_category_id']) {
                    $stepCategoryTaskCount++;
                }
                if ($item['tro_repair_step_category_id'] != $item['roat_step_category_id']) {
                    $upstreamStepCategoryTaskCount++;
                }
            }
            return ['upstream_step_category_task_count' => $stepCategoryTaskCount, 'step_category_task_count' => $upstreamStepCategoryTaskCount];
        }
        $stepCategoryTaskList = [];
        $upstreamStepCategoryTaskList = [];
        if ($dataType == 'list') {
            foreach ($list as $item) {
                if ($item['tro_repair_step_category_id'] == $item['roat_step_category_id']) {
                    $stepCategoryTaskList[] = $item['task_id'];
                }
                if ($item['tro_repair_step_category_id'] != $item['roat_step_category_id']) {
                    $upstreamStepCategoryTaskList[] = $item['task_id'];
                }
            }
            return ['upstream_step_category_task_list' => $stepCategoryTaskList, 'step_category_task_list' => $upstreamStepCategoryTaskList];
        }
        return ['upstream_step_category_task_count' => 0, 'step_category_task_count' => 0];
    }
}
